package com.rf.richfitwheel.workflow.service.impl;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.SqlAdapterMapper;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.dao.DelegationDao;
import com.rf.richfitwheel.workflow.entity.Delegation;
import com.rf.richfitwheel.workflow.service.DelegationService;

import net.sf.json.JSONObject;

@Service
@Transactional
public class DelegationServiceImpl implements DelegationService {
	@Autowired
	private DelegationDao delegationDao;
	@Autowired
	private SqlAdapterMapper sqlAdapterMapper;// 注入dao

	// 添加授权记录
	@Override
	public void addDelegation(Delegation delegation) {
//		try {
			delegation.setId(UuidUtil.get32UUID());
			delegationDao.addDelegation(delegation);
//			return R.ok();
//		} catch (Exception e) {
//			e.printStackTrace();
//			return R.error();
//		}
	}

	@Override
	public void updateDelegation(Delegation delegation) {
//		try {
			delegationDao.updateDelegation(delegation);
//			return R.ok();
//		} catch (Exception e) {
//			e.printStackTrace();
//			return R.error();
//		}
	}

	// 查询授权信息
	@Override
	public Delegation findById(String id) {
		Delegation delegation = delegationDao.findById(id);
		return delegation;
	}

	// 查询分页列表
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		try {

			/*StringBuffer sql = new StringBuffer(
					" select d.id,d.auth_id as authId,d.be_auth_id as beAuthId,DATE_FORMAT(d.start_time,'%Y-%m-%d') as startTime,DATE_FORMAT(d.end_time,'%Y-%m-%d') as endTime, d.auth_catagory as authCatagory, d.reason as reason from wf_delegation d ");*/
			PageUtils pageUtils = PageUtils.getPageInfo(params);
			Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
			Delegation delegation = (Delegation) JSONObject.toBean(JSONObject.fromObject(params),
					Delegation.class);
			if(delegation != null && StringUtils.isNotBlank(delegation.getAuthId())) {
				delegation.setAuthId("%" + delegation.getAuthId().trim() + "%");
			}else {
				delegation.setAuthId(null);
			}
			if(delegation != null && StringUtils.isNotBlank(delegation.getBeAuthId())) {
				delegation.setBeAuthId("%" + delegation.getBeAuthId().trim() + "%");
			}else {
				delegation.setBeAuthId(null);
			}
			List<Map<String, Object>> mapList =  delegationDao.selectPageByPar(delegation);
			Integer count = delegationDao.count(delegation);
			/*if (params.get("beAuthId") != null && !"".equals(params.get("beAuthId"))) {
				sql.append(
						"left join sys_user u on u.id= d.be_auth_id left join sys_user s on s.id=d.auth_id where u.username like '%"
								+ params.get("beAuthId") + "%'");
				if (params.get("authId") != null && !"".equals(params.get("authId"))) {
					sql.append("and s.username like '%" + params.get("authId") + "%'");
				}
			} else {
				if (params.get("authId") != null && !"".equals(params.get("authId"))) {
					sql.append("left join sys_user s on s.id=d.auth_id where s.username like '%" + params.get("authId")
							+ "%'");
				}

			}

//			if(delegation!=null){
//				sql.append(" where 1=1");
//			}
			// sql.append(" order by class_code,data_code ");
			sqlAdapterMapper.selectSQL(sql.toString());
			pageUtils = new PageUtils(p);
			List<Map<String, String>> mapList = (List<Map<String, String>>) pageUtils.getList();
			for (Map<String, String> map : mapList) {
				String beAuthId = (String) map.get("beAuthId");
				User user = userMapper.selectByPrimaryKey(beAuthId);
				map.put("beAuthId", user.getUsername());
				// 将登陆用户作为授权人显示在列表中
				// String userId = (String)
				// SecurityUtils.getSubject().getSession().getAttribute(Constant.USERSESSIONID);
				String authId = (String) map.get("authId");
				User user2 = userMapper.selectByPrimaryKey(authId);
				map.put("authId", user2 == null ? null : user2.getUsername());

			}*/
			pageUtils.setList(mapList);
			pageUtils.setTotalCount(count);
			return pageUtils;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 删除授权记录
	@Override
	public void deleteDelegation(String[] ids) {
		if (ids == null) {
//			return R.error("没有选择要删除的记录。");
			throw new BusinessException("没有选择要删除的记录");
		}
//		try {
			for (String id : ids) {
				delegationDao.deleteDelegation(id);
			}
//			return R.ok();
//		} catch (Exception e) {
//			e.printStackTrace();
//			return R.error();
//		}

	}
	@Override
	public List<Delegation> getDelegationByUserId(String userId){
		Delegation delegation=new Delegation();
		delegation.setBeAuthId(userId);
		//将查询时间处理为00:00:00
		Calendar cal2 = Calendar.getInstance();
		cal2.set(Calendar.HOUR_OF_DAY, 0);
		cal2.set(Calendar.MINUTE, 0);
		cal2.set(Calendar.SECOND, 0);
		cal2.set(Calendar.MILLISECOND, 0);
		delegation.setDealTime(cal2.getTime());
		return delegationDao.getDelegationByUserId(delegation);
	}

	/**
	 * 根据用户id和时间查询授权记录
	 * 
	 * @param pp
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getDelegationByUserIdAndTime(Map<String, Object> pp) {
		return delegationDao.getDelegationByUserIdAndTime(pp);
	}



}
